package com.example.pei.papa.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;

import com.example.pei.papa.view.IdentifyingCodeView;
import com.example.pei.papa.R;
public class register_step_two_Activity extends AppCompatActivity implements View.OnClickListener,IdentifyingCodeView.InputCompleteListener {

    private IdentifyingCodeView icv;
    private Button button_nextstep_three;
    private Button button_back_to_first;
    private Button button_send_again;
    private String phone;
    private String code;

    //每次验证请求需要间隔35S
    private int time=60;
    //控制按钮样式是否改变
    private boolean tag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_step_two_);
        ViewInit();
        changeBtnGetCode();
        Intent intent=getIntent();
        phone=intent.getStringExtra("phoneString");
    }


    //重写按键监听
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_to_step_three:btn_to_step_three_Click();break;
            case R.id.btn_back_to_first:finish();break;
            case R.id.btn_send_again:btn_send_again_Click() ;break;
        }
    }

    static int i=0;
    @Override
    public void inputComplete() {
        if(++i==6) {
            button_nextstep_three.setBackgroundColor(Color.parseColor("#12B7F5"));
            button_nextstep_three.setClickable(true);
        }
    }
    @Override
    public void deleteContent() {
        if(--i!=6) {
            button_nextstep_three.setBackgroundColor(Color.parseColor("#e9ebec"));
            button_nextstep_three.setClickable(false);
        }
    }

    private void ViewInit(){
        button_nextstep_three=(Button)findViewById(R.id.btn_to_step_three);
        button_back_to_first=(Button)findViewById(R.id.btn_back_to_first);
        button_send_again=(Button)findViewById(R.id.btn_send_again) ;
        icv=(IdentifyingCodeView)findViewById(R.id.icv);

        //设置监听
        button_nextstep_three.setOnClickListener(this);
        button_back_to_first.setOnClickListener(this);
        button_send_again.setOnClickListener(this);
        icv.setInputCompleteListener(this);

        button_nextstep_three.setClickable(false);

    }
    private void btn_to_step_three_Click(){
        code=icv.getTextContent();
        BmobSMS.verifySmsCode(register_step_two_Activity.this,phone ,code, new VerifySMSCodeListener() {
            @Override
            public void done(cn.bmob.sms.exception.BmobException e) {
                if(e==null){
                    //短信验证码验证成功
                    Intent intent=new Intent("register_step_3_Activity");
                    intent.putExtra("phoneString",phone);
                    startActivity(intent);
                }else{
                    Toast.makeText(register_step_two_Activity.this,"验证码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void changeBtnGetCode() {
        Thread thread = new Thread() {
            @Override
            public void run() {
                if (tag) {
                    while (time > 0) {
                        time--;
                        //如果活动为空
                        if (register_step_two_Activity.this == null) {
                            break;
                        }
                        register_step_two_Activity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                button_send_again.setText("重新发送(" + time + ")");
                                button_send_again.setClickable(false);
                                button_send_again.setTextColor(Color.parseColor("#aaaaaa"));
                            }
                        });
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    tag = false;
                }
                time = 60;
                tag = true;

                if (register_step_two_Activity.this != null) {
                    register_step_two_Activity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            button_send_again.setText("重新发送");
                            button_send_again.setClickable(true);
                            button_send_again.setTextColor(Color.parseColor("#333333"));
                        }
                    });
                }
            }
        };
        thread.start();
    }
    private void btn_send_again_Click(){
        changeBtnGetCode();
        //发送验证码
        BmobSMS.requestSMSCode(register_step_two_Activity.this, phone, "交友神器",new RequestSMSCodeListener() {
            @Override
            public void done(Integer smsId, cn.bmob.sms.exception.BmobException e) {
                if (e == null) {
                    //验证码发送成功
                }
                else{
                    Toast.makeText(register_step_two_Activity.this,"发送失败 Code:"+e.getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
